package com.king.core.nio;

import java.nio.ByteBuffer;
import java.nio.InvalidMarkException;

/**
 * 描述: 通过ByteBuffer来验证一些基本条件
 *
 * @author : sujinchen
 * @date : 2020/11/8
 */
public class ByteBufferDemo {
    ByteBuffer byteBuffer;

    public ByteBufferDemo(ByteBuffer byteBuffer) {
        this.byteBuffer = byteBuffer;
    }

    public static void main(String[] args) {
        ByteBufferDemo demo = new ByteBufferDemo(ByteBuffer.wrap(new byte[]{1, 2, 3, 4, 5, 6}));
        //capacity不能为负数
        demo.test1();
        //limit不能为负数
        demo.test2();
        //position不能为负数
        demo.test3();
        //position不能大于limit
        demo.test4();
        //limit不能大于capacity
        demo.test5();
        //position<mark mark被丢弃
        demo.test6();
        //limit<mark mark被丢弃
        demo.test7();
        //position>新的limit 则position=limit
        demo.test8();
        //当position和limit一样时，写入数据会出现异常
        demo.test9();
    }



    /**
     * capacity不能为负数
     */
    public void test1() {
        try {
            byteBuffer = ByteBuffer.allocate(-1);
        } catch (IllegalArgumentException e) {
            System.out.println("capacity不能为负数");
            e.printStackTrace();
        }
    }

    /**
     * limit不饿能为负数
     */
    public void test2() {
        try {
            byteBuffer.limit(-1);
        } catch (IllegalArgumentException e) {
            System.out.println("limit不能为负数");
            e.printStackTrace();
        }
    }

    /**
     * position不能为负数
     */
    public void test3() {
        try {
            byteBuffer.position(-1);
        } catch (Exception e) {
            System.out.println("position不能为负数");
            e.printStackTrace();
        }
    }

    /**
     * position不能大于limit
     */
    public void test4() {
        byteBuffer.limit(2);
        try {
            byteBuffer.position(3);
        } catch (Exception e) {
            System.out.println("position不能大于limit");
            e.printStackTrace();
        }
    }

    /**
     * limit不能大于capacity
     */
    public void test5() {
        try {
            byteBuffer.limit(7);
        } catch (IllegalArgumentException e) {
            System.out.println("limit不能大于capacity");
            e.printStackTrace();
        }
    }

    /**
     * 如果定义了mark，则在将position调整为小于该mark的值时，该mark被丢弃
     */
    public void test6() {
        byteBuffer = ByteBuffer.wrap(new byte[]{1, 2, 3, 4, 5, 6});
        byteBuffer.position(3);
        byteBuffer.mark();
        byteBuffer.position(1);
        try {
            byteBuffer.reset();
        } catch (InvalidMarkException e) {
            System.out.println("mark值被丢弃");
            e.printStackTrace();
        }
    }

    public void test7() {
        byteBuffer = ByteBuffer.wrap(new byte[]{1, 2, 3, 4, 5, 6});
        byteBuffer.limit(4);
        byteBuffer.position(2);
        byteBuffer.mark();
        byteBuffer.limit(1);
        try {
            byteBuffer.reset();
        } catch (InvalidMarkException e) {
            System.out.println("mark值被丢弃");
            e.printStackTrace();
        }
    }

    public void test8() {
        byteBuffer = ByteBuffer.wrap(new byte[]{1, 2, 3, 4, 5, 6});
        byteBuffer.position(4);
        byteBuffer.limit(2);
        System.out.println("新的限制：" + byteBuffer.limit());
    }

    public void test9() {
        byteBuffer = ByteBuffer.wrap(new byte[]{1, 2, 3, 4, 5, 6});
        byteBuffer.limit(2);
        byteBuffer.position(2);
        try {
            byteBuffer.put(new byte[]{2, 3});
        } catch (Exception e) {
            System.out.println("写入数据异常");
            e.printStackTrace();
        }

    }
}
